home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2352 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  50 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie question:  Is this code OK?
  5. Date: 20 Jan 1996 19:56:15 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4drhcv$cr@news.iag.net>
  8. References: <4dmebk$foq@pegasus.interpac.net> <4doajv$ov0$2@mhafn.production.compuserve.com>
  9. NNTP-Posting-Host: pm1-orl30.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4doajv$ov0$2@mhafn.production.compuserve.com>, 
  13. 71046.623@CompuServe.COM says...
  14. >
  15. >Why not just
  16. >
  17. >        while (*string)
  18. >           if (isspace(*string++)
  19. >              numwords++;
  20. >
  21.  
  22. Because, it will not behave, when odd spacing is encountered (and
  23. it is missing a ')' >:-).
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27.  
  28. int main()
  29.    {
  30.    char *string, ary[] = " test  string  with  odd  spacing ";
  31.    int numwords = 0;
  32.  
  33.    string  = ary;
  34.    while (*string)
  35.       if (isspace(*string++)) /* missing ) */
  36.          numwords++;
  37.  
  38.    printf( "There are %d words in: \"%s\"\n", numwords, ary);
  39.    return 0;
  40.    }
  41.  
  42. Output:
  43.  
  44. There are 10 words in: " test  string  with  odd  spacing "
  45.  
  46. -- 
  47. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  48. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  49.  
  50.